package com.dfbz.springboot_mybatisplus;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dfbz.springboot_mybatisplus.entity.HospitalMeta;
import com.dfbz.springboot_mybatisplus.mapper.HospitalMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class Demo01 {
    @Autowired
    private HospitalMapper hospitalMapper;

    @Test
    public void testSelect() {
        // 传递null代表查询全部
        List<HospitalMeta> userList = hospitalMapper.selectList(null);
        System.err.println(userList);
        for (HospitalMeta user : userList) {
            System.out.println(user);
        }
    }

    /**
     * mp的第一部分 传统 mapper接口 抽象方法 (接口中写抽象方法)->mapper.xml(xml或注解中写sql)-->service(service中调用方法)--->controller(controller中调用方法)
     * mp：  自定义mapper  继承basemapper  操作user--->对应泛型user
     *
     * 第二部分
     * 注解
     * @TableName("hospitalmeta")  指定表名
     * @TableId(value = "hospitalId", type = IdType.AUTO) 指定主键 以及主键类型
     * @TableField("mysqlStatus")    表名和实体映射
     *
     * 第三部分
     * 排除非表字段的三种方式
     *  private transient String source   使用transient关键字    代表不序列化了
     *  private static String source   使用 static关键字       类的属性了 不属于实例对象了
     * @TableField(value = "mysqlStatus", exist = false)   exist 默认为true  false 表中不存在
     */
    @Test
    public void inster() {
        HospitalMeta hospitalMeta = new HospitalMeta();
        hospitalMeta.setHospitalName("南京医院");
        hospitalMeta.setHospitalAddress("南京市建邺区");
        hospitalMeta.setMysqlUrl("localhost:3306");
        hospitalMeta.setMysqlSchema("nanjingyiyuan");
        hospitalMeta.setMysqlUsername("root");
        hospitalMeta.setMysqlPassword("root");
//        hospitalMeta.setMysqlStatus(1);
        int rows = hospitalMapper.insert(hospitalMeta);
        System.err.println(rows);

    }

    /**
     * 普通查询
     */
    @Test
    public void selectById() {
        HospitalMeta hospitalMeta = hospitalMapper.selectById(2);
        System.err.println(hospitalMeta);
    }

    @Test
    public void selectByIds() {
        List<HospitalMeta> hospitalMetas = hospitalMapper.selectBatchIds(Arrays.asList(3, 4));
        hospitalMetas.forEach(System.err::println);
    }

    @Test
    public void selectByMap() {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("hospitalName","北京医院");
        //key  是数据库列名  value是值
        List<HospitalMeta> hospitalMetas = hospitalMapper.selectByMap(stringObjectHashMap);
        hospitalMetas.forEach(System.err::println);
    }

    /**
     * 条件构造器
     * QueryWrapper<HospitalMeta> queryWrapper = new QueryWrapper<HospitalMeta>();
     * <>泛型 要操作的实体
     * QueryWrapper<Object> queryWrapper = Wrappers.query();
     * 上面两个效果等效
     */
    @Test
    public void selectByWrapper() {
        /**
         * 查询医院名字包含京  状态小于1的
         * new QueryWrapper()         Warppers.query()
         */
        QueryWrapper<HospitalMeta> queryWrapper = new QueryWrapper<HospitalMeta>();
//        QueryWrapper<Object> queryWrapper = Wrappers.query();
        queryWrapper.like("hospitalName","京").lt("mysqlStatus",1);
        List<HospitalMeta> hospitalMetas = hospitalMapper.selectList(queryWrapper);
        hospitalMetas.forEach(System.err::println);
    }

    @Test
    public void selectByWrapper2() {
        /**
         * 查询医院名字包含京  状态小于等于1 大于等于0  并且url不为null的
         * new QueryWrapper()         Warppers.query()
         */
        QueryWrapper<HospitalMeta> hospitalMetaQueryWrapper = new QueryWrapper<>();
        hospitalMetaQueryWrapper.like("hospitalName","京");
        hospitalMetaQueryWrapper.between("mysqlStatus",0,1);
        hospitalMetaQueryWrapper.isNotNull("mysqlUrl");
        List<HospitalMeta> hospitalMetas = hospitalMapper.selectList(hospitalMetaQueryWrapper);
        hospitalMetas.forEach(System.out::println);
    }
}
